package com.kiln.juxingqs.ui.vm

import android.app.Application
import androidx.lifecycle.MutableLiveData
import client.global.commlib.network.data.JsonResult
import client.global.commlib.network.data.Ret
import client.global.commlib.utils.StoreUtil
import com.kiln.juxingqs.api.bean.GoodsDetail
import com.kiln.juxingqs.api.bean.TradeGoodsDetail
import com.kiln.juxingqs.api.bean.UserInfo
import com.kiln.juxingqs.ui.viewmodel.MiddleVm

class LotDealModel(app: Application) : MiddleVm(app) {

    val goodsDetail = MutableLiveData<JsonResult<GoodsDetail>>()
    val tradeGoodsDetail = MutableLiveData<JsonResult<TradeGoodsDetail>>()
    val isShowing = MutableLiveData<Boolean>(false)
    val buyRet = MutableLiveData<JsonResult<Ret>>()
    val callUser = MutableLiveData<JsonResult<UserInfo>>()

    fun userInfo() {
        hold({
            server.userinfo().apply {
                body()?.let {
                    if (it.isSuccess) {
                        StoreUtil.saveBean(getApplication(), it.data!!)
                    }
                }
            }
        }, success = {
            callUser.value = it
        })
    }

    fun holdTradeCall(id: String) {
        hold({
            server.trade_detail(id, "")
        }, {
        }, success = {
            goodsDetail.value = it
        }, complete = {
        })
    }

    fun holdGoodsDetailCall(id: String) {
        hold({
            server.trade_goods_detail(id)
        }, {
        }, success = {
            tradeGoodsDetail.value = it
        }, complete = {
        })
    }

    fun holdBuyYj(detail: TradeGoodsDetail, num: String) {
        hold({
            server.trade_goods_buy_yj(
                "${ detail.goods_dict.goods_id}",
                "${ detail.goods_dict.price}",
                num
            )
        }, {
            isShowing.value = true
        }, success = {
            buyRet.value = it
        }, complete = {
            isShowing.value = false

        })
    }

    fun holdBuyTj(detail: TradeGoodsDetail, num: String) {
        hold({
            server.trade_goods_buy_tj(
                "${detail.goods_dict.goods_id}",
                "${detail.goods_dict.price_pf}",
                num
            )
        }, {
            isShowing.value = true
        }, success = {
            buyRet.value = it
        }, complete = {
            isShowing.value = false

        })
    }

    fun holdBuyYh(detail: TradeGoodsDetail, num: String) {
        hold({
            server.trade_goods_buy_yh(
                "${detail.goods_dict.goods_id}",
                "${detail.goods_dict.price_pf}",
                num
            )
        }, {
            isShowing.value = true
        }, success = {
            buyRet.value = it
        }, complete = {
            isShowing.value = false

        })
    }

}